import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lmmi/common/index.dart';
import 'package:lmmi/view/time_capsule_detail/page.dart';

void main() {
  group('TimeCapsuleDetailPage Tests', () {
    testWidgets('should show loading indicator initially', (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        ProviderScope(
          child: MaterialApp(
            home: TimeCapsuleDetailPage(timeCapsuleId: 'test-id'),
          ),
        ),
      );

      // 验证显示加载指示器
      expect(find.byType(CircularProgressIndicator), findsOneWidget);
      expect(find.text('Time Capsule Detail'), findsOneWidget);
    });

    testWidgets(
      'should show not found message when time capsule does not exist',
      (WidgetTester tester) async {
        await tester.pumpWidget(
          ProviderScope(
            child: MaterialApp(
              home: TimeCapsuleDetailPage(timeCapsuleId: 'non-existent-id'),
            ),
          ),
        );

        // 等待加载完成
        await tester.pumpAndSettle();

        // 验证显示未找到消息
        expect(find.text('Time capsule not found'), findsOneWidget);
      },
    );

    testWidgets('should display time capsule content when found', (
      WidgetTester tester,
    ) async {
      // 使用示例数据中的第一个时间胶囊ID
      await tester.pumpWidget(
        ProviderScope(
          child: MaterialApp(home: TimeCapsuleDetailPage(timeCapsuleId: 'tc1')),
        ),
      );

      // 等待加载完成
      await tester.pumpAndSettle();

      // 验证显示时间胶囊内容
      expect(find.textContaining('Dear future me'), findsOneWidget);
      expect(find.text('Time Capsule Detail'), findsOneWidget);
    });

    testWidgets('should show back button and handle navigation', (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        ProviderScope(
          child: MaterialApp(home: TimeCapsuleDetailPage(timeCapsuleId: 'tc1')),
        ),
      );

      // 等待加载完成
      await tester.pumpAndSettle();

      // 验证返回按钮存在
      expect(find.byIcon(Icons.arrow_back_ios_new_rounded), findsOneWidget);

      // 点击返回按钮
      await tester.tap(find.byIcon(Icons.arrow_back_ios_new_rounded));
      await tester.pumpAndSettle();

      // 验证页面被关闭（在测试环境中，Navigator.pop会关闭页面）
    });

    testWidgets('should display image when time capsule has image', (
      WidgetTester tester,
    ) async {
      // 创建一个带图片的测试时间胶囊
      final testTimeCapsule = TimeCapsule(
        id: 'test-with-image',
        content: 'Test content with image',
        userId: '1',
        createDate: '2024-07-25T10:00:00.000Z',
        openDate: '2025-07-25T10:00:00.000Z',
        description: 'Test description',
        isOpen: false,
        email: 'test@example.com',
        backupEmail: 'backup@example.com',
        imagePath: '/test/image/path.jpg',
      );

      // 注意：在实际测试中，我们需要模拟TimeCapsuleService.getTimeCapsuleById
      // 这里只是展示测试结构
      await tester.pumpWidget(
        ProviderScope(
          child: MaterialApp(
            home: TimeCapsuleDetailPage(timeCapsuleId: 'test-with-image'),
          ),
        ),
      );

      // 等待加载完成
      await tester.pumpAndSettle();

      // 在实际实现中，这里会验证图片组件的存在
      // expect(find.byType(Image), findsOneWidget);
    });
  });
}
